package cn.com.b2office.util;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import jeasy.analysis.MMAnalyzer;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.cn.ChineseAnalyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.LockObtainFailedException;

import cn.com.b2office.bean.ItemQueryParam;
import cn.com.b2office.dao.CategoryDao;
import cn.com.b2office.dao.ItemDao;
import cn.com.b2office.domain.Category;
import cn.com.b2office.domain.Item;
import cn.com.b2office.test.dao.BaseTestCase;

/**
 * @author zhenggang
 * @version Jul 28, 2008 10:59:54 AM comment
 */
public class CreatSearchIndex extends BaseTestCase {

	private ItemDao itemDao = null;

	private CategoryDao categoryDao = null;

	public void setItemDao(ItemDao itemDao) {
		this.itemDao = itemDao;
	}

	public void setCategoryDao(CategoryDao categoryDao) {
		this.categoryDao = categoryDao;
	}

	public void testQueryItems() throws CorruptIndexException,
			LockObtainFailedException, IOException {
		ItemQueryParam param = new ItemQueryParam();
		param.setPageSize(10000);
		List<Item> list = itemDao.queryItems(param);
		// 把类别名字查询出来放进list里
		for (Object obj : list) {
			Item tempItem = new Item();
			tempItem = (Item) obj;
			// tempItem.setItem_clsName(categoryDao.getCategory(
			// tempItem.getItem_clsno()).getItem_clsname());
		}

		indexItem(list); // 对取出的数据创建索引
	}

	/**
	 * 创建索引
	 * 
	 * @throws IOException
	 * @throws LockObtainFailedException
	 * @throws CorruptIndexException
	 */
	public void indexItem(List list) throws CorruptIndexException,
			LockObtainFailedException, IOException {

		IndexWriter writer = new IndexWriter("d://index", getAnalyzer(), true); // true表示下次建立索引时会清除这次建立的索引重新建立索引,如果为false则表示追加索引,在原来索引的基础上追加
		for (int i = 0; i < list.size(); i++) {
			Item item = new Item();
			item = (Item) list.get(i);
			// System.out.println(item.getItem_clsName()+"
			// "+item.getTitles()+""+item.getComments());
			Document doc = new Document();

			doc.add(new Field("itemId", item.getItem_no(), Field.Store.YES,
					Field.Index.TOKENIZED));
			doc.add(new Field("item_clsno", item.getItem_clsno(),
					Field.Store.YES, Field.Index.TOKENIZED));
			// doc.add(new
			// Field("item_clsname",item.getItem_clsName(),Field.Store.YES,Field.Index.TOKENIZED));
			doc.add(new Field("titles", item.getTitles(), Field.Store.YES,
					Field.Index.TOKENIZED));
			doc.add(new Field("price", String.valueOf(item.getB2hprice()),
					Field.Store.YES, Field.Index.TOKENIZED));
			doc.add(new Field("deliverday", String
					.valueOf(item.getDeliverday()), Field.Store.YES,
					Field.Index.TOKENIZED));
			doc.add(new Field("point_rate", String
					.valueOf(item.getPoint_rate()), Field.Store.YES,
					Field.Index.TOKENIZED));
			doc.add(new Field("unit_no", item.getUnit_no(), Field.Store.YES,
					Field.Index.TOKENIZED));
			doc.add(new Field("usenewunit", String
					.valueOf(item.getUsenewunit()), Field.Store.YES,
					Field.Index.TOKENIZED));
			if (item.getNewunit() != null) {
				doc.add(new Field("newunit", item.getNewunit(),
						Field.Store.YES, Field.Index.TOKENIZED));
			} else {
				doc.add(new Field("newunit", "", Field.Store.YES,
						Field.Index.TOKENIZED));
			}
			doc.add(new Field("useshape", String.valueOf(item.getUseshape()),
					Field.Store.YES, Field.Index.TOKENIZED));
			doc.add(new Field("bigqty", Float.toString(item.getBigqty()),
					Field.Store.YES, Field.Index.TOKENIZED));
			doc.add(new Field("midqty", Float.toString(item.getMidqty()),
					Field.Store.YES, Field.Index.TOKENIZED));
			doc.add(new Field("smlqty", Float.toString(item.getSmlqty()),
					Field.Store.YES, Field.Index.TOKENIZED));

			if (item.getComments() != null) {
				doc.add(new Field("comment", item.getComments(),
						Field.Store.YES, Field.Index.TOKENIZED));
			} else {
				doc.add(new Field("comment", "", Field.Store.YES,
						Field.Index.TOKENIZED));
			}
			// 为内容建立索引但不存储
			/*
			 * if(item.getComments() !=null){ doc.add(new Field("comment", new
			 * StringReader(item.getComments()))); }else{ doc.add(new
			 * Field("comment", new StringReader(""))); }
			 */
			writer.addDocument(doc);
		}
		System.out.println("有 " + list.size() + " 条记录");
		writer.close(); // 关闭输入索引
	}

	/** 创建分析器 */
	public Analyzer getAnalyzer() {
		return new StandardAnalyzer();
		// return new ChineseAnalyzer();
		// return new MMAnalyzer();
	}
}
